﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;

namespace SOEN341_SCHEDULER_PHASE1
{
    [Serializable()]
    /// <summary>
    /// Object that defines a list of constraints of student
    /// </summary>
    class Constraint : ISerializable
    {
        /// <summary>
        /// List of CustomDateTime
        /// </summary>
        private List<CustomDateTime> _listOfConstraints;

        /// <summary>
        /// Deserialization 
        /// </summary>
        /// <param name="info"></param>
        /// <param name="ctxt"></param>
        public Constraint(SerializationInfo info, StreamingContext ctxt)
        {
            _listOfConstraints = (List<CustomDateTime>)info.GetValue("liconstraint", typeof(List<CustomDateTime>));
        }

        /// <summary>
        /// Serialization 
        /// </summary>
        /// <param name="info"></param>
        /// <param name="ctxt"></param>
        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("liconstraint", _listOfConstraints);
        }
        /// <summary>
        /// Default Constructor
        /// </summary>
        public Constraint()
        {
            _listOfConstraints = new List<CustomDateTime>();
        }


        /// <summary>
        /// Parameterized Constructor
        /// </summary>
        /// <param name="listOfConstraints">List of constraints to copy</param>
        public Constraint(List<CustomDateTime> listOfConstraints)
        {
            _listOfConstraints = new List<CustomDateTime>();
            CustomDateTime[] arrCdt = listOfConstraints.ToArray();

            for (int i = 0; i < arrCdt.Length; i++)
            {
                _listOfConstraints.Add(listOfConstraints[i]);
            }
        }

        /// <summary>
        /// Get list of constraints
        /// </summary>
        /// <returns>The list of constraints</returns>
        public List<CustomDateTime> GetConstraints()
        {
            return this._listOfConstraints;
        }

        /// <summary>
        /// Add a constraint to list
        /// </summary>
        /// <param name="newConstraint">Constraint to add - NO COLLISION CHECK IS MADE</param>
        public void AddConstraint(CustomDateTime newConstraint)
        {
            _listOfConstraints.Add(newConstraint);
        }

        /// <summary>
        /// Removes a constraint from the list
        /// </summary>
        /// <param name="toRem">CustomDateTime object to remove</param>
        /// <returns>TRUE: REMOVED - FALSE: ERROR</returns>
        public bool RemoveConstraint(CustomDateTime toRem)
        {
            CustomDateTime[] arrCdt = _listOfConstraints.ToArray();
            for (int i = 0; i < arrCdt.Length; i++)
            {
                //MessageBox.Show(arrCdt[i].GetTimeInterval() + " &&&&\n" + toRem.GetTimeInterval());   
                if (arrCdt[i].Equals(toRem))
                {
                    _listOfConstraints.Remove(arrCdt[i]);
                    return true;
                }
            }
            return false;
        }
    }
}
